package com.yilin.tms.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.commons.exception.BusinessException;
import com.yilin.commons.util.MapUtil;
import com.yilin.tms.core.entity.user.hatch.*;
import com.yilin.tms.user.mapper.hatch.*;
import com.yilin.tms.user.service.IHatchService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class HatchService implements IHatchService {

    @Resource
    private HatchAlipayMapper hatchAlipayDao;
    @Resource
    private HatchBaiduOcrMapper hatchBaiduOcrDao;
    @Resource
    private HatchBaiduFaceMapper hatchBaiduFaceDao;
    @Resource
    private HatchBaiduMapMapper hatchBaiduMapDao;
    @Resource
    private HatchGaodeMapMapper hatchGaodeMapDao;
    @Resource
    private HatchEtcMapper hatchEtcDao;
    @Resource
    private HatchJpushMapper hatchJpushDao;
    @Resource
    private HatchSmsMapper hatchSmsDao;
    @Resource
    private HatchPlatLinksMapper hatchPlatLinksDao;
    @Resource
    private HatchTmsInterfaceMapper hatchTmsInterfaceDao;
    @Resource
    private HatchWechatMapper hatchWechatDao;
    @Resource
    private HatchHybMapper hatchHybDao;

    @Resource
    private HatchProcessMapper hatchProcessDao;
    @Resource
    private HatchPricingRuleMapper hatchPricingRuleMapper;
    @Resource
    private HatchPricingRuleItemMapper hatchPricingRuleItemMapper;

    @Override
    public List<HatchPlatProduct> getAllPlatProductHatch() {
        QueryWrapper<HatchPlatProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("plat_name");
        queryWrapper.isNotNull("product_code");
        queryWrapper.isNotNull("product_name");
        queryWrapper.isNotNull("shipper_app_name");
        queryWrapper.isNotNull("trucker_app_name");
        return hatchPlatLinksDao.selectList(queryWrapper);
    }

    @Override
    public HatchAlipay getHatchAlipayByPlatId(String platAccountId) {
        return hatchAlipayDao.selectOne(new QueryWrapper<HatchAlipay>().eq("plat_id", platAccountId));
    }

    @Override
    public HatchBaiduOcr getHatchBaiduOcrByPlatId(String platAccountId) {
        return hatchBaiduOcrDao.selectOne(new QueryWrapper<HatchBaiduOcr>().eq("plat_id", platAccountId));
    }

    @Override
    public HatchBaiduFace getHatchBaiduFaceByPlatId(String platAccountId) {
        return hatchBaiduFaceDao.selectOne(new QueryWrapper<HatchBaiduFace>().eq("plat_id", platAccountId));
    }

    @Override
    public HatchBaiduMap getHatchBaiduMapByPlatId(String platId) {
        return hatchBaiduMapDao.selectOne(new QueryWrapper<HatchBaiduMap>().eq("plat_id", platId));
    }

    @Override
    public HatchGaodeMap getHatchGaodeMapByPlatId(String platId) {
        return hatchGaodeMapDao.selectOne(new QueryWrapper<HatchGaodeMap>().eq("plat_id", platId));
    }

    @Override
    public HatchEtc getHatchEtcByPlatId(String platId) {
        return hatchEtcDao.selectOne(new QueryWrapper<HatchEtc>().eq("plat_id", platId));
    }

    @Override
    public HatchJpush getHatchJpushByPlatId(String platId) {
        return hatchJpushDao.selectOne(new QueryWrapper<HatchJpush>().eq("plat_id", platId));
    }

    @Override
    public HatchSms getHatchSmsByPlatId(String platId) {
        return hatchSmsDao.selectOne(new QueryWrapper<HatchSms>().eq("plat_id", platId));
    }

    @Override
    public HatchPlatProduct getHatchPlatProductByPlatId(String platId) {
        return hatchPlatLinksDao.selectOne(new QueryWrapper<HatchPlatProduct>().eq("plat_id", platId));
    }

    @Override
    public HatchPlatProduct getHatchPlatProductByProductCode(String productCode) {
        return hatchPlatLinksDao.selectOne(new QueryWrapper<HatchPlatProduct>().eq("product_code", productCode));
    }

    @Override
    public HatchTmsInterface getHatchTmsInterfaceByPlatId(String platId) {
        return hatchTmsInterfaceDao.selectOne(new QueryWrapper<HatchTmsInterface>().eq("plat_id", platId));
    }

    @Override
    public HatchWechat getHatchWechatByPlatId(String platId) {
        return hatchWechatDao.selectOne(new QueryWrapper<HatchWechat>().eq("plat_id", platId));
    }

    @Override
    public HatchHyb getHatchHybMapperByPlatId(String platId) {
        return hatchHybDao.selectOne(new QueryWrapper<HatchHyb>().eq("plat_id", platId));
    }


    @Override
    public HatchProcess getHatchProcessByPlatId(String platId) {
        HatchProcess selectOne = hatchProcessDao.selectOne(new QueryWrapper<HatchProcess>().eq("plat_id", platId));
        if (selectOne!=null){
            selectOne.setProcessStep(selectOne.getProcessStatus().index);
            List<Map<String,Object>> displayProcessList = new ArrayList<>();
            Arrays.asList(HatchProcess.ProcessStatus.values()).forEach(processStatus -> displayProcessList.add(MapUtil.generate(map -> {
                map.put("name",processStatus.name());
                map.put("index",processStatus.index);
                map.put("title",processStatus.name);
                map.put("info",processStatus.info);
            })));
            selectOne.setDisplayProcessList(displayProcessList);
        }
        return selectOne;
    }

    @Override
    public void editHatchAlipay(HatchAlipay hatchAlipay) {
        if (hatchAlipay.getId() == null) {
            hatchAlipay.setCreateTime(new Date());
            hatchAlipayDao.insert(hatchAlipay);
        } else {
            hatchAlipay.setUpdateTime(new Date());
            hatchAlipayDao.updateById(hatchAlipay);
        }
    }

    @Override
    public void editHatchBaiduOcr(HatchBaiduOcr hatchBaiduOcr) {
        if (hatchBaiduOcr.getId() == null) {
            hatchBaiduOcr.setCreateTime(new Date());
            hatchBaiduOcrDao.insert(hatchBaiduOcr);
        } else {
            hatchBaiduOcr.setUpdateTime(new Date());
            hatchBaiduOcrDao.updateById(hatchBaiduOcr);
        }
    }

    @Override
    public void editHatchBaiduFace(HatchBaiduFace hatchBaiduFace) {
        if (hatchBaiduFace.getId() == null) {
            hatchBaiduFace.setCreateTime(new Date());
            hatchBaiduFaceDao.insert(hatchBaiduFace);
        } else {
            hatchBaiduFace.setUpdateTime(new Date());
            hatchBaiduFaceDao.updateById(hatchBaiduFace);
        }
    }

    @Override
    public void editHatchBaiduMap(HatchBaiduMap hatchBaiduMap) {
        if (hatchBaiduMap.getId() == null) {
            hatchBaiduMap.setCreateTime(new Date());
            hatchBaiduMapDao.insert(hatchBaiduMap);
        } else {
            hatchBaiduMap.setUpdateTime(new Date());
            hatchBaiduMapDao.updateById(hatchBaiduMap);
        }
    }

    @Override
    public void editHatchGaodeMap(HatchGaodeMap hatchGaodeMap) {
        if (hatchGaodeMap.getId() == null) {
            hatchGaodeMap.setCreateTime(new Date());
            hatchGaodeMapDao.insert(hatchGaodeMap);
        } else {
            hatchGaodeMap.setUpdateTime(new Date());
            hatchGaodeMapDao.updateById(hatchGaodeMap);
        }
    }

    @Override
    public void editHatchEtc(HatchEtc hatchEtc) {
        if (hatchEtc.getId() == null) {
            hatchEtc.setCreateTime(new Date());
            hatchEtcDao.insert(hatchEtc);
        } else {
            hatchEtc.setUpdateTime(new Date());
            hatchEtcDao.updateById(hatchEtc);
        }
    }

    @Override
    public void editHatchJpush(HatchJpush hatchJpush) {
        if (hatchJpush.getId() == null) {
            hatchJpush.setCreateTime(new Date());
            hatchJpushDao.insert(hatchJpush);
        } else {
            hatchJpush.setUpdateTime(new Date());
            hatchJpushDao.updateById(hatchJpush);
        }
    }

    @Override
    public void editHatchSms(HatchSms hatchSms) {
        if (hatchSms.getId() == null) {
            hatchSms.setCreateTime(new Date());
            hatchSmsDao.insert(hatchSms);
        } else {
            hatchSms.setUpdateTime(new Date());
            hatchSmsDao.updateById(hatchSms);
        }
    }

    @Override
    public void editPlatProduct(HatchPlatProduct hatchPlatProduct) {
        if (hatchPlatProduct.getId() == null) {
            hatchPlatProduct.setCreateTime(new Date());
            hatchPlatLinksDao.insert(hatchPlatProduct);
        } else {
            hatchPlatProduct.setUpdateTime(new Date());
            hatchPlatLinksDao.updateById(hatchPlatProduct);
        }
    }

    @Override
    public void editHatchTmsInterface(HatchTmsInterface hatchTmsInterface) {
        if (hatchTmsInterface.getId() == null) {
            hatchTmsInterface.setCreateTime(new Date());
            hatchTmsInterfaceDao.insert(hatchTmsInterface);
        } else {
            hatchTmsInterface.setUpdateTime(new Date());
            hatchTmsInterfaceDao.updateById(hatchTmsInterface);
        }
    }

    @Override
    public void editHatchWechat(HatchWechat hatchWechat) {
        if (hatchWechat.getId() == null) {
            hatchWechat.setCreateTime(new Date());
            hatchWechatDao.insert(hatchWechat);
        } else {
            hatchWechat.setUpdateTime(new Date());
            hatchWechatDao.updateById(hatchWechat);
        }
    }

    @Override
    public void editHatchHyb(HatchHyb hatchHyb) {
        if (hatchHyb.getId() == null) {
            hatchHyb.setCreateTime(new Date());
            hatchHybDao.insert(hatchHyb);
        } else {
            hatchHyb.setUpdateTime(new Date());
            hatchHybDao.updateById(hatchHyb);
        }
    }

    @Override
    public void editHatchProcess(HatchProcess hatchProcess) {
        if (hatchProcess.getId() == null) {
            hatchProcess.setCreateTime(new Date());
            hatchProcessDao.insert(hatchProcess);
        } else {
            hatchProcess.setCompleteTime(new Date());
            hatchProcess.setUpdateTime(new Date());
            hatchProcessDao.updateById(hatchProcess);
        }
    }

    @Override
    public List<HatchPricingRule> getCommTruckTypeRule(String platId, String commTruckTypeId) {
        QueryWrapper<HatchPricingRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plat_id", platId);
        queryWrapper.eq("comm_truck_type_id", commTruckTypeId);
        List<HatchPricingRule> hatchPricingRules = hatchPricingRuleMapper.selectList(queryWrapper);
        for (HatchPricingRule pricingRule : hatchPricingRules) {
            QueryWrapper<HatchPricingRuleItem> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("pricing_rule_id", pricingRule.getId());
            queryWrapper2.orderByAsc("start_distance");
            List<HatchPricingRuleItem> itemList = hatchPricingRuleItemMapper.selectList(queryWrapper2);
            pricingRule.setRuleItems(itemList);
        }
        return hatchPricingRules;
    }

    @Override
    public void saveOrUpdateRule(HatchPricingRule carrier) {
        if (carrier.getCommTruckTypeId() == null) throw new BusinessException("通用车型错误");
        if (carrier.getRuleItems().size() < 2) throw new BusinessException("规则阶梯不得低于2项");
        for (HatchPricingRuleItem item :carrier.getRuleItems()){
            if (item.getIsUnit()==null) throw new BusinessException("计价规则方式为空");
            if (item.getEndNoLimited()==null) throw new BusinessException("数据不合规【endNoLimited】");
            if (item.getDisabled()==null) throw new BusinessException("数据不合规【disabled】");
        }
        //变更规则
        if (carrier.getId() == null) {
            hatchPricingRuleMapper.insert(carrier);
        } else {
            hatchPricingRuleMapper.updateById(carrier);
        }
        //全部删除规则项
        QueryWrapper<HatchPricingRuleItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pricing_rule_id", carrier.getId());
        hatchPricingRuleItemMapper.delete(queryWrapper);
        //重新生產規則
        for (HatchPricingRuleItem item :carrier.getRuleItems()){
            item.setPricingRuleId(carrier.getId());
            hatchPricingRuleItemMapper.insert(item);
        }
    }
}
